package com.newasia.user.services;

import com.newasia.commons.tool.exception.ValidateException;
import com.newasia.user.ISysRoleService;
import com.newasia.user.constants.SysRetCodeConstants;
import com.newasia.user.converter.SysRoleConverter;
import com.newasia.user.dal.entitys.SysRole;
import com.newasia.user.dal.entitys.SysUser;
import com.newasia.user.dal.entitys.SysUserRole;
import com.newasia.user.dal.persistence.SysRoleMapper;
import com.newasia.user.dal.persistence.SysUserMapper;
import com.newasia.user.dto.RoleUserRequest;
import com.newasia.user.dto.RoleUserResponse;
import com.newasia.user.dto.SysRoleDto;
import com.newasia.user.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SysRoleServiceImpl implements ISysRoleService{

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleConverter sysRoleConverter;

    @Override
    public List<SysRoleDto> selectSysRoleByUserId(Long user_id) {
        log.info("Begin SysRoleServiceImpl.selectSysRoleByUserId: user_id:"+user_id);

        List<SysRoleDto> sysRoleDtoList = new ArrayList<SysRoleDto>();
        SysUser sysUser = new SysUser();
        sysUser.setUserId(user_id);
        try{
            List<SysRole> sysRoles = sysRoleMapper.selectSysRoleByUserId(sysUser);
            if(sysRoles.size()<0){
                return null;
            }
            sysRoleDtoList = sysRoleConverter.role2List(sysRoles);
        }catch (Exception e){
            log.error("SysRoleServiceImpl.selectSysRoleByUserId Occur Exception :"+e);
        }

        return sysRoleDtoList;
    }

    @Override
    public RoleUserResponse insertSysUserRole(RoleUserRequest roleUserRequest) {
        log.info("Begin SysRoleServiceImpl.insertSysUserRole: request:"+roleUserRequest);
        RoleUserResponse response=new RoleUserResponse();
        try {
            SysUser user = sysUserMapper.selectUserByName(roleUserRequest.getUserNmae());

            SysRole sysRole = new SysRole();
            sysRole.setRoleId(roleUserRequest.getRoleId());
            SysRole role = sysRoleMapper.selectSysRoleByName(sysRole);

            SysUserRole userRole = new SysUserRole();
            userRole.setRoleId(role.getRoleId());
            userRole.setUserId(user.getUserId());

            validUserRole(user,role);
            if(sysRoleMapper.insertSysUserRole(userRole)!=1){
                response.setCode(SysRetCodeConstants.ROLE_INSERT_ERROR.getCode());
                response.setMsg(SysRetCodeConstants.ROLE_INSERT_ERROR.getMessage());
                return response;
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("SysRoleServiceImpl.insertSysUserRole Occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public RoleUserResponse updateSysUserRole(RoleUserRequest roleUserRequest) {
        log.info("Begin SysRoleServiceImpl.updateSysUserRole: request:"+roleUserRequest);
        RoleUserResponse response=new RoleUserResponse();
        try {
            SysUser user = sysUserMapper.selectUserByName(roleUserRequest.getUserNmae());

            SysRole sysRole = new SysRole();
            sysRole.setRoleId(roleUserRequest.getRoleId());
            SysRole role = sysRoleMapper.selectSysRoleByName(sysRole);

            SysUserRole userRole = new SysUserRole();
            userRole.setRoleId(role.getRoleId());
            userRole.setUserId(user.getUserId());

            validUserRole(user,role);
            if(sysRoleMapper.updateSysRoleByUserId(userRole)!=1){
                response.setCode(SysRetCodeConstants.ROLE_UPDATE_ERROR.getCode());
                response.setMsg(SysRetCodeConstants.ROLE_UPDATE_ERROR.getMessage());
                return response;
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("SysRoleServiceImpl.updateSysUserRole Occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    private void validUserRole(SysUser user,SysRole role){
        List<SysRole> roles = sysRoleMapper.selectSysRoleByUserId(user);
        for (SysRole roleEntity: roles){
            if(roleEntity.getRoleId()==role.getRoleId()||
                    roleEntity.getRoleId().equals(role.getRoleId())){
                throw new ValidateException(SysRetCodeConstants.ROLE_REPEAT_ERROR.getCode(),
                        SysRetCodeConstants.ROLE_REPEAT_ERROR.getMessage());
            }
        }
    }
}
